package com.financing.platform.web.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.SourceProductDTO;
import com.financing.platform.service.ISourceProductService;
import com.financing.platform.util.DTOdateSet;

/**
 * 
 * @ClassName: sourceProductAction
 *
 */
@Controller("sourceProductAction")
@RequestMapping("/api")
@Scope("prototype")
public class SourceProductAction extends BaseAction{
	
	@Autowired
	private ISourceProductService sourceProductService;
	
	/**
	 * 
	 * @Title: addSourceProduct
	 * @Description:绑定产品关联来源
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/sourceProduct/add.shtml")
	@ResponseBody
	public ResultMap addSourceProduct(HttpServletRequest request,
			HttpServletResponse response,SourceProductDTO sourceProduct) {
		this.log.info("begin addSourceProduct info!");
		try{
			if(sourceProduct==null||StringUtils.isBlank(sourceProduct.getMediaSource())
					||StringUtils.isBlank(sourceProduct.getProductCode())||StringUtils.isBlank(sourceProduct.getProductType())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			List<Map<String, String>> querySourceProduct = sourceProductService.querySourceProduct(sourceProduct);
			if(querySourceProduct!=null&&querySourceProduct.size()>0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			
			// 初始化数据赋值
			DTOdateSet.setCreateInfo(sourceProduct, request.getSession());
			DTOdateSet.setUpdateInfo(sourceProduct, request.getSession());
			sourceProduct.setDeleteTag("1");
			try {
				sourceProductService.addSourceProduct(sourceProduct);
			} catch (Exception e) {
				this.log.info("add error",e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			this.log.error("error addSourceProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end addSourceProduct info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: querySourceProduct
	 * @Description:查询绑定产品关联来源
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/sourceProduct/query.shtml")
	@ResponseBody
	public ResultMap querySourceProduct(HttpServletRequest request,
			HttpServletResponse response,SourceProductDTO sourceProduct) {
		this.log.info("begin querySourceProduct info!");
		try{
			if(sourceProduct==null||StringUtils.isBlank(sourceProduct.getMediaSource())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			 List<Map<String, String>> list = sourceProductService.querySourceProduct(sourceProduct);
			 resultMap.setAttribute("list",list);
			 resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			this.log.error("error querySourceProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end querySourceProduct info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: querySourceProduct
	 * @Description:是否已经添加绑定产品关联来源
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/sourceProduct/replayAdd.shtml")
	@ResponseBody
	public ResultMap replayAdd(HttpServletRequest request,
			HttpServletResponse response,SourceProductDTO sourceProduct) {
		this.log.info("begin replayAdd info!");
		try{
			if(sourceProduct==null||StringUtils.isBlank(sourceProduct.getMediaSource())
					||StringUtils.isBlank(sourceProduct.getProductCode())||StringUtils.isBlank(sourceProduct.getProductType())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			List<Map<String, String>> querySourceProduct = sourceProductService.querySourceProduct(sourceProduct);
			if(querySourceProduct!=null&&querySourceProduct.size()>0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			 resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			this.log.error("error replayAdd",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end replayAdd info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: delSourceProduct
	 * @Description:绑定产品关联来源
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/sourceProduct/del.shtml")
	@ResponseBody
	public ResultMap delSourceProduct(HttpServletRequest request,
			HttpServletResponse response,SourceProductDTO sourceProduct) {
		
		this.log.info("begin delSourceProduct info!");
		
		try{
			if(sourceProduct==null||StringUtils.isBlank(sourceProduct.getId())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			sourceProductService.deleteSourceProduct(sourceProduct);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			this.log.error("error delSourceProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end delSourceProduct info access !");
		return resultMap;
	}
	
}
